package com.education.platform.management.service.impl;

import com.education.platform.common.enums.AuditTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.ArticleDao;
import com.education.platform.dao.CourseCommentDao;
import com.education.platform.dao.CourseDao;
import com.education.platform.dao.UserBaseDao;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.entity.CourseCommentDO;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.CourseCommentService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* describe: 课程评论
* @author : tomato
* @date: 2020-06-29 10:28:07.323
**/
@Service
public class CourseCommentServiceImpl extends BaseServiceImpl<CourseCommentDao, CourseCommentDO> implements CourseCommentService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CourseCommentServiceImpl.class);

    @Autowired
	private CourseCommentDao courseCommentDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private UserBaseDao userBaseDao;
    @Autowired
    private PushUtil pushUtil;

    /**
     * 精选
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void selectedComments(List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<CourseCommentDO> commentDOS  = new ArrayList<>(ids.size());
        CourseCommentDO commentDO;
        for(Integer id:ids){
            commentDO = new CourseCommentDO();
            commentDO.setId(id);
            commentDO.setUpdateTime(new Date());
            commentDO.setAuditStatus(AuditTypeEnum.AUDIT.getValue());
            //加精选时  默认为已读
            commentDO.setReadStatus(true);
            commentDO.setUpdateBy(ThreadUser.getUserName());
            commentDOS.add(commentDO);
        }
        courseCommentDao.updateInBatch(commentDOS);

        List<CourseCommentDO> list = courseCommentDao.getListByIds(ids);
        List<CourseDO> courseDOS = new ArrayList<>();
        CourseDO courseDO;
        List<ArticleDO> articleDOS = new ArrayList<>();
        ArticleDO articleDO;
        //文章  课程  的精选总数增加
        for(CourseCommentDO courseCommentDO :list){
            //课程评论
          if(courseCommentDO.getTopicType() ==1){
              courseDO = new CourseDO();
              courseDO.setId(courseCommentDO.getCourseId());
              courseDO.setSelectedCommentCount(1);
              courseDOS.add(courseDO);
          }else{//文章评论
              articleDO = new ArticleDO();
              articleDO.setId(courseCommentDO.getCourseId());
              articleDO.setSelectedCommentCount(1);
              articleDOS.add(articleDO);
          }
        }
        if(CollectionUtils.isNotEmpty(courseDOS)){
            courseDao.updateCountBatch(courseDOS);
        }
        if(CollectionUtils.isNotEmpty(articleDOS)){
            articleDao.updateCountBatch(articleDOS);
        }

        //推送消息
//        PushUtil pushUtil = PushUtil.getInstance();
        for(CourseCommentDO i:list){
            pushUtil.push(i.getUserId(), PushTypeEnum.COMMENT,String.valueOf(i.getCourseId()),
                    (i.getTopicType()!=null&&i.getTopicType()==1)?"course":"article");
        }
    }
}
